import 'package:flutter/material.dart';
import 'package:bitsdojo_window/bitsdojo_window.dart';
import 'widgets/nav_drawer.dart';
import 'widgets/player_bar.dart';
import 'pages/recommend_page.dart';
import 'pages/discovery_page.dart';
import 'pages/podcast_page.dart';
import 'pages/roaming_page.dart';
import 'pages/follow_page.dart';
import 'pages/my_music_page.dart';
import 'pages/recent_page.dart';
import 'pages/my_podcast_page.dart';

void main() {
  runApp(const MyApp());
  
  doWhenWindowReady(() {
    // 设置窗口属性
    appWindow.minSize = const Size(1100, 700);
    appWindow.size = const Size(1280, 800);
    appWindow.alignment = Alignment.center;
    appWindow.show();
  });
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '网易云音乐',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(
          seedColor: Colors.red,
          brightness: Brightness.light,
        ),
        useMaterial3: true,
        fontFamily: 'Microsoft YaHei',
      ),
      home: const MainPage(),
    );
  }
}

class MainPage extends StatefulWidget {
  const MainPage({super.key});

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  int _selectedIndex = 0;

  final List<Widget> _pages = [
    const RecommendPage(),
    const DiscoveryPage(),
    const PodcastPage(),
    const RoamingPage(),
    const FollowPage(),
    const MyMusicPage(),
    const RecentPage(),
    const MyPodcastPage(),
  ];

  void _onNavTap(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFF5F5F7),
      body: Row(
        children: [
          // 左侧导航栏
          NavDrawer(
            selectedIndex: _selectedIndex,
            onTap: _onNavTap,
          ),
          // 右侧主内容区
          Expanded(
            child: Container(
              margin: const EdgeInsets.only(top: 8, right: 8, bottom: 8),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(12),
              ),
              child: Column(
                children: [
                // 顶部功能栏区域（总高度100px，一行布局）
                Container(
                  height: 50,
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: const BorderRadius.only(
                      topLeft: Radius.circular(12),
                      topRight: Radius.circular(12),
                    ),
                  ),
                  child: MoveWindow(
                    child: Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 16),
                      child: Row(
                        children: [
                          // 左侧功能区：导航按钮 + 搜索框 + 用户信息
                          Expanded(
                            child: Row(
                              children: [
                                // 左侧导航按钮
                                IconButton(
                                  icon: const Icon(Icons.arrow_back_ios_new, size: 18, color: Colors.grey),
                                  onPressed: () {},
                                ),
                                IconButton(
                                  icon: const Icon(Icons.arrow_forward_ios, size: 18, color: Colors.grey),
                                  onPressed: () {},
                                ),
                                const SizedBox(width: 20),
                                // 搜索框
                                Container(
                                  width: 300,
                                  height: 32,
                                  decoration: BoxDecoration(
                                    color: const Color(0xFFF5F5F7),
                                    borderRadius: BorderRadius.circular(16),
                                  ),
                                  child: Row(
                                    children: [
                                      const SizedBox(width: 12),
                                      const Icon(Icons.search, size: 18, color: Colors.grey),
                                      const SizedBox(width: 8),
                                      Expanded(
                                        child: TextField(
                                          decoration: const InputDecoration(
                                            hintText: '搜索',
                                            hintStyle: TextStyle(color: Colors.grey, fontSize: 14),
                                            border: InputBorder.none,
                                            contentPadding: EdgeInsets.symmetric(vertical: 8),
                                          ),
                                          textAlignVertical: TextAlignVertical.center,
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                                const Spacer(),
                                // 右侧用户区
                                Container(
                                  padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
                                  decoration: BoxDecoration(
                                    color: Colors.orange,
                                    borderRadius: BorderRadius.circular(12),
                                  ),
                                  child: const Text(
                                    'SVIP会员',
                                    style: TextStyle(color: Colors.white, fontSize: 12),
                                  ),
                                ),
                                const SizedBox(width: 10),
                                const CircleAvatar(
                                  radius: 16,
                                  backgroundColor: Colors.grey,
                                  child: Icon(Icons.person, color: Colors.white, size: 18),
                                ),
                                const SizedBox(width: 8),
                                const Text('桂远翔', style: TextStyle(fontSize: 14, fontWeight: FontWeight.w500)),
                                const SizedBox(width: 10),
                                IconButton(
                                  icon: const Icon(Icons.mail_outline, size: 18, color: Colors.grey),
                                  onPressed: () {},
                                ),
                                IconButton(
                                  icon: const Icon(Icons.settings_outlined, size: 18, color: Colors.grey),
                                  onPressed: () {},
                                ),
                              ],
                            ),
                          ),
                          // 右侧窗口控制区：只有WindowTitleBarBox包裹窗口控制按钮
                          WindowTitleBarBox(
                            child: Row(
                              mainAxisSize: MainAxisSize.min,
                              children: [
                                MinimizeWindowButton(
                                  colors: WindowButtonColors(iconNormal: Colors.grey),
                                ),
                                MaximizeWindowButton(
                                  colors: WindowButtonColors(iconNormal: Colors.grey),
                                ),
                                CloseWindowButton(
                                  colors: WindowButtonColors(iconNormal: Colors.grey),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
                // 主内容区
                Expanded(
                  child: Container(
                    margin: const EdgeInsets.all(8),
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(12),
                      boxShadow: [
                        BoxShadow(
                          color: Colors.black.withOpacity(0.1),
                          blurRadius: 8,
                          offset: const Offset(0, 2),
                        ),
                      ],
                    ),
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(12),
                      child: _pages[_selectedIndex],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
      // 底部播放器
      bottomNavigationBar: const PlayerBar(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // TRY THIS: Try changing the color here to a specific color (to
        // Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
        // change color while the other colors stay the same.
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          // Column is also a layout widget. It takes a list of children and
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
          //
          // Column has various properties to control how it sizes itself and
          // how it positions its children. Here we use mainAxisAlignment to
          // center the children vertically; the main axis here is the vertical
          // axis because Columns are vertical (the cross axis would be
          // horizontal).
          //
          // TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
          // action in the IDE, or press "p" in the console), to see the
          // wireframe for each widget.
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text('You have pushed the button this many times:'),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
